﻿var online = new Array();

var CSPContactWay = function () {
    this.objName = "";
    this.isFinded = false; /*是否已经找到在线联系方式 */
    this.contactWays; /*联系方式列表 1代表qq 0 代表阿里旺旺 [{"Address":"619381","Type":1},{"Address":"hl_8792","Type":0}]*/
    this.contactLink; /*联系方式链接  #id 格式*/
    this.contactImg; /*联系方式图片 #id 格式*/
    this.Init = function () {
        var that = this;
        var qqFilePath = 'http://' + 'webpresence.qq.com/getonline?' + encodeURI('Type=1&');
        var taobaoFilePath = 'http://' + 'amos.alicdn.com/muliuserstatus.aw?beginnum=0&site=cntaobao&charset=utf-8&callback=' + this.objName + '.SetContact&uids=';
        var taobaoi = 0;
        $.each(this.contactWays, function (idex, item) {
            /*1代表是QQ*/
            if (item.Type == 1) {
                qqFilePath += item.Address + ":";
            }
            /*0代表淘宝*/
            if (item.Type == 0) {
                if (taobaoi == 0) {
                    taobaoFilePath += encodeURI(item.Address);
                } else {
                    taobaoFilePath += ";" + encodeURI(item.Address);
                }

                taobaoi++;
            }
        });
        //$.includeFile([qqFilePath]);
        $.ajax({
            type: "get",
            url: qqFilePath,
            cache: false,
            dataType: "script",
            async: false,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //var bbb="";
            },
            success: function (data, textStatus, jqXHR) {
                that.SetContact();
            }
        });
        if (this.isFinded) {
            return;
        }
        $.ajax({
            type: "get",
            url: taobaoFilePath,
            cache: false,
            dataType: "script",
            async: false,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var bbb = "";
            },
            success: function (data, textStatus, jqXHR) {
                //that.SetContact();
            }
        });
        //            document.write("<script src='"+taobaoFilePath+"'></"+"script>");
        //$.getScript(qqFilePath);
        if (!this.isFinded) {
            this.SetContactByAddressAndType(this.contactWays[0].Address, this.contactWays[0].Type);
        }


    }
    this.SetContact = function (taobaoResponse) {
        if (this.isFinded) {
            return;
        }
        for (var i = 0; i < online.length; i++) {
            /*online*/
            if (online[i] == 1) {
                var address = this.GetDataByQQ(i);
                this.SetContactByAddressAndType(address, 1);
                this.isFinded = true;
                return;
            }
        }
        if (taobaoResponse == undefined) {
            return;
        }
        for (var i = 0; i < taobaoResponse.data.length; i++) {
            if (taobaoResponse.data[i] == 1) {
                var address = this.GetDataByTaoBao(i);
                this.SetContactByAddressAndType(address, 0);
                this.isFinded = true;
                return;
            }
        }
    }
    this.GetDataByQQ = function (index) {
        var result;
        var i = 0;
        $.each(this.contactWays, function (idex, item) {
            /*1代表是QQ*/
            if (item.Type == 1) {
                if (i == index) {
                    result = item.Address;
                    return;
                }
                i++;
            }

        });
        return result;
    }
    this.GetDataByTaoBao = function (index) {
        var result;
        var i = 0;
        $.each(this.contactWays, function (idex, item) {
            /*0代表是淘宝*/
            if (item.Type == 0) {
                if (i == index) {
                    result = item.Address;
                    return;
                }
                i++;
            }

        });
        return result;
    }
    this.SetContactByAddressAndType = function (address, type) {
        if (type == 1) {
            var qqhref = "http://" + "wpa.qq.com/msgrd?v=3&amp;uin=" + encodeURI(address) + "&amp;site=qq&amp;menu=yes";
            var qqsrc = "http://" + "wpa.qq.com/pa?p=2:" + encodeURI(address) + ":41";
            $(this.contactLink).attr("href", qqhref);
            $(this.contactImg).attr("src", qqsrc);
        } else if (type == 0) {
            var taobaohref = "http://" + "www.taobao.com/webww/ww.php?ver=3&touid=" + encodeURI(address) + "&siteid=cntaobao&status=1&charset=utf-8";
            var taobaosrc = "http://" + "amos.alicdn.com/realonline.aw?v=2&uid=" + encodeURI(address) + "&site=cntaobao&s=1&charset=utf-8";
            $(this.contactLink).attr("href", taobaohref);
            $(this.contactImg).attr("src", taobaosrc);
        }
    }
}